﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace OpenNETCF.Barcode
{
    public partial class LuminanceChart : UserControl
    {
        private byte[] m_data;

        public byte[] Data 
        {
            get { return m_data; }
            set
            {
//                if (this.Width < m_data.Length)
//                {
//                    this.Width = m_data.Length;
//                }

                m_data = value;

                Refresh();
            }
        }

        public LuminanceChart()
        {
            InitializeComponent();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.FillRectangle(new SolidBrush(Color.White), new Rectangle(0, 0, this.Width, this.Height));

            if (Data == null)
            {
                e.Graphics.DrawString("No Data", this.Font, SystemBrushes.ControlDark, 0, 0);
                return;
            }

            int dataWidth = this.Width / Data.Length;

            int offset = 0;
            int height;
            int stride = 1;

            if (this.Width < m_data.Length)
            {
                stride = m_data.Length / this.Width;
                dataWidth = 1;
            }

            using(var blackBrush = new SolidBrush(Color.Black))
            {
                for (int i = 0; i < Data.Length; i+= stride)
                {
                    offset = i * dataWidth;
                    height = 0xff - Data[i];

                    e.Graphics.FillRectangle(blackBrush, offset, this.Height - height, dataWidth, height);
                }
            }
        }
    }
}
